import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:my_shop/cache_event_identification.dart';
import 'package:my_shop/load_my_goods.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({
    Key? key,
  }) : super(key: key);

  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  final TextEditingController userNameController = TextEditingController();
  final TextEditingController passwordController = TextEditingController();
  final TextEditingController secondPasswordController =
      TextEditingController();

  List<UserModel> allAccount = [];

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color(SpUtil.getInt(Constant.mainColor) ?? MyColor.mainColor.value),
        title: const Text('Register'),
      ),
      body: Container(
        padding: const EdgeInsets.symmetric(horizontal: 30),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Card(
              color: Color(SpUtil.getInt(Constant.mainColor) ?? MyColor.mainColor.value),
              child: Container(
                padding: const EdgeInsets.all(16),
                child: Column(
                  children: [
                    const Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Text(
                          'Welcome to register an account',
                          style: TextStyle(
                              fontSize: 18, fontWeight: FontWeight.bold),
                        )
                      ],
                    ),
                    const SizedBox(height: 16),
                    TextField(
                      controller: userNameController,
                      decoration: InputDecoration(
                        labelText: 'username:',
                        hintText: 'Please input username',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        contentPadding: const EdgeInsets.symmetric(
                          vertical: 12,
                          horizontal: 16,
                        ),
                      ),
                    ),
                    const SizedBox(height: 16),
                    TextField(
                      controller: passwordController,
                      decoration: InputDecoration(
                        labelText: 'password:',
                        hintText: 'Please input password',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        contentPadding: const EdgeInsets.symmetric(
                          vertical: 12,
                          horizontal: 16,
                        ),
                        suffixIcon: const Icon(Icons.lock),
                      ),
                      obscureText: true,
                    ),
                    const SizedBox(height: 16),
                    TextField(
                      controller: secondPasswordController,
                      decoration: InputDecoration(
                        labelText: 'Repeat password:',
                        hintText: 'Please re-enter the password to confirm',
                        border: OutlineInputBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        contentPadding: const EdgeInsets.symmetric(
                          vertical: 12,
                          horizontal: 16,
                        ),
                        suffixIcon: passwordController.text ==
                                secondPasswordController.text
                            ? const Icon(Icons.check, color: Colors.green)
                            : const Icon(Icons.close, color: Colors.red),
                      ),
                      obscureText: true,
                    ),
                    const SizedBox(height: 20),
                    Container(
                      decoration: BoxDecoration(
                        color: const Color(0xFFF8961E),
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: Row(
                        children: [
                          Expanded(
                            child: TextButton(
                              onPressed: register,
                              child: const Text(
                                'Register',
                                style: TextStyle(color: Colors.black),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  void register() {
    List<UserModel> allUserAccount = SpUtil.getObjList<UserModel>(
            Constant.userModelList,
            (v) => UserModel.fromJson(v as Map<String, dynamic>)) ??
        <UserModel>[];
    List<String> allUserList =
        allUserAccount.map((e) => e.userName ?? '').toList();
    if (userNameController.text.isEmpty) {
      Fluttertoast.showToast(msg: "Please enter username");
    } else if (passwordController.text.isEmpty) {
      Fluttertoast.showToast(msg: "Please enter password");
    } else if (secondPasswordController.text.isEmpty) {
      Fluttertoast.showToast(msg: "Please re-enter the password to confirm");
    } else if (allUserList.contains(userNameController.text)) {
      Fluttertoast.showToast(msg: "This account already exists");
    } else if (passwordController.text != secondPasswordController.text) {
      Fluttertoast.showToast(
          msg: "The two passwords are inconsistent. Please re-enter them");
    } else {
      if (allUserAccount.isEmpty) {
        allUserAccount = [
          UserModel(
            password: passwordController.text,
            userName: userNameController.text,
          )
        ];
      } else {
        allUserAccount.add(UserModel(
          password: passwordController.text,
          userName: userNameController.text,
        ));
      }

      SpUtil.putObjectList(Constant.userModelList, allUserAccount);
      userNameController.clear();
      passwordController.clear();
      secondPasswordController.clear();
      Fluttertoast.showToast(
          msg:
              "Registration is successful, please go to the login page to login");
      Navigator.pop(context);
    }
  }
}
